package com.cskaoyan.service;

import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.roleBo.RoleBo2;
import com.cskaoyan.bean.roleBo.RolePermissionsBo;
import com.cskaoyan.bean.roleVo.*;
import com.cskaoyan.mapper.MarketMyRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author Austin Hsu
 * @Date 2022/9/9 22:30
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    MarketMyRoleMapper marketMyRoleMapper;

    @Override
    public void insert(RoleBo2 bo) {          //用postman构造请求 扒数据进数据库
        List<RoleBo2.Data.SystemPermissions> systemPermissions = bo.getData().getSystemPermissions();
        for (RoleBo2.Data.SystemPermissions systemPermission : systemPermissions) {
            List<RoleBo2.Data.SystemPermissions.ChildrenX> children = systemPermission.getChildren();
            for (RoleBo2.Data.SystemPermissions.ChildrenX child : children) {
                List<RoleBo2.Data.SystemPermissions.ChildrenX.Children> children2 = child.getChildren();
                for (RoleBo2.Data.SystemPermissions.ChildrenX.Children child2 : children2) {
                    marketMyRoleMapper.insert(systemPermission, child, child2);
                }
            }
        }
    }

    @Override
    public void insert2(String[] s) {
        marketMyRoleMapper.insert2(s);
    }

    //------------------------------------业务代码--------------------------------------
    @Override
    public CommonData<MarketRole> query(MarketRole marketRole, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        String name = marketRole.getName();      // like
        String sort = info.getSort();          //sort by add_time
        String order = info.getOrder();          // desc/asc

        List<MarketRole> list;
        if (name != null && !"".equals(name)) {   //名字非空
            list = marketMyRoleMapper.query(name, sort, order);
        } else {
            list = marketMyRoleMapper.query2(sort, order);
        }

        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(list);
        Integer total = (int) marketRolePageInfo.getTotal();
        Integer pages = marketRolePageInfo.getPages();

        CommonData<MarketRole> marketRoleCommonData = new CommonData<>(total, pages, info.getLimit(), info.getPage(), list);

        return marketRoleCommonData;
    }

    @Override
    public MarketRole create(MarketRole marketRole) {
        marketRole.setAddTime(new Date());
        marketRole.setUpdateTime(new Date());
        marketMyRoleMapper.insert3(marketRole);
//        marketRole.getId();  //已经自动返回这个id了
        marketRole.setEnabled(true);
        marketRole.setDeleted(false);
        return marketRole;

       /* RoleCreateVo data = marketMyRoleMapper.insert3(marketRole);
        return data;*/
    }

    @Override
    public RoleVo permissions(Integer roleId) {

        //----------权限------------

        Integer i = roleId;
        List<String> assignedPermissions =
                marketMyRoleMapper.queryAssignedPermissionsByRoleId(i);  //通过roleid查这个人拥有的权限


//-----------------------------------框架list3!!!!-----------------------------------------------------
        List<RolePermissionsVo> list = marketMyRoleMapper.queryRolePermissions();   //以cata1为分类，查询cata1和cata2

        List<SystemPermissions> list3 = new ArrayList<>();
        for (RolePermissionsVo rolePermissionsVo : list) {
            List<Children1> list1 = new ArrayList<>();

            SystemPermissions systemPermissions = new SystemPermissions();

            Children2 children2 = new Children2();

            String cata1 = rolePermissionsVo.getCata1();   //父类 eg：商场管理
            String cata2 = rolePermissionsVo.getCata2();   //子类 eg：关键词,品牌管理,类目管理,订单管理,通用问题
            String[] split = cata2.split(",");

            for (String s : split) {
                List<Children2> list2 = marketMyRoleMapper.queryApiIdByCata2(s);      //-----!!!最里面的Bean
                Children1 children1 = new Children1();
                children1.setChildren(list2);
                children1.setId(s);
                children1.setLabel(s);        //关键词，品牌管理····
                list1.add(children1);
            }
            systemPermissions.setChildren(list1);
            systemPermissions.setLabel(cata1);
            systemPermissions.setId(cata1);
            list3.add(systemPermissions);

        }
//-----------------------------------框架-----------------------------------------------------
        RoleVo data = new RoleVo();
        data.setSystemPermissions(list3);
        data.setAssignedPermissions(assignedPermissions);

        return data;
    }


    /**
     * 修改授权post
     * 1.先根据roleid把这个角色的所有api全部删除
     * 2.再根据请求插入
     *
     * @param rolePermissionsBo
     * @return
     */
    @Override
    public RoleVo permissions2(RolePermissionsBo rolePermissionsBo) {
        int roleId = rolePermissionsBo.getRoleId();
        marketMyRoleMapper.deletePermissionByRoleId(roleId);

        for (String permission : rolePermissionsBo.getPermissions()) {
            marketMyRoleMapper.insertPermissionsByRoleId(permission, roleId);
        }


        return null;
    }

    @Override
    public RoleCreateVo update(MarketRole marketRole) {
        marketRole.setUpdateTime(new Date());
        marketMyRoleMapper.updateRole(marketRole);
        return null;
    }

    @Override
    public RoleCreateVo delete(MarketRole marketRole) {
        marketMyRoleMapper.deleteRole(marketRole);
        return null;
    }

    @Override
    public CommonData<RoleOptionVo> options() {
        CommonData<RoleOptionVo> data = new CommonData<>();
        List<RoleOptionVo> list = marketMyRoleMapper.queryRoleOptions();    //每个成员的名字
        int total = marketMyRoleMapper.queryCountRoleOptions();           //成员总数 count(*)
        data.setLimit(total);
        data.setTotal(total);
        data.setPage(1);
        data.setPages(1);
        data.setList(list);
        return data;
    }
}
